home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 26 / AMIGAplus Sonderheft 26 (2000)(Falke)(DE)(Track 1 of 2)[!].iso / Updates / HD-Installer / -WHDLoad- / WHDLoad_dev / Src / programs / WCmp.c < prev    next >
C/C++ Source or Header  |  1999-03-29  |  3KB  |  125 lines

  1. /*
  2.     a little, simple, quick cmp program
  3.     advantages against the thousend other cmp's out there:
  4.         - command line only
  5.         - wide output hex And ascii (this was the reason for writing)
  6.         - does not need to load whole file at once
  7.         - std c, should be eatable by every compiler
  8.  
  9.     released under GNU Public License
  10.     wepl, sometime ago ...
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #define strcasecmp stricmp
  18.  
  19. #define buflen 32768/2
  20. #define error1 { perror(argv[1]); exit(20); }
  21. #define error2 { perror(argv[2]); exit(20); }
  22. #define maxpl 16    /* displayed diffs per line */
  23.  
  24. int opt_quick=1;
  25.  
  26. /**********************/
  27.  
  28. void cmpout(unsigned char *c1, unsigned char *c2, int p1, int p2, int len) {
  29.   int j;
  30.   unsigned char *t;
  31.  
  32.   if (opt_quick == 0) return;
  33.   
  34.   printf("%06x ",p1);                /* file1 offset */
  35.   for (t=c1,j=0;j<len;j++) printf("%02x",*t++);
  36.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  37.   for (j=0;j<len;j++,c1++) putchar(*c1 < ' ' || *c1 > 127 ? '.' : *c1);
  38.   for (j=0;j<=maxpl-len;j++) putchar(' ');
  39.   if  (p1 != p2) printf("%06x ",p2);        /* file2 offset */
  40.   for (t=c2,j=0;j<len;j++) printf("%02x",*t++);
  41.   for (j=0;j<=2*(maxpl-len);j++) putchar(' ');
  42.   for (j=0;j<len;j++,c2++) putchar(*c2 < ' ' || *c2 > 127 ? '.' : *c2);
  43.   putchar('\n');
  44. }
  45.  
  46. /**********************/
  47.  
  48. int cmp(char *m1, char *m2, int o1, int o2, int len) {
  49.   int i, diffs=0;
  50.   char t1[maxpl+1]="", t2[maxpl+1]="";
  51.   int dc=0;    /* count of bytes in buff */
  52.   int ds=0;    /* filepos of first stored byte in buff */
  53.   
  54.   for (i=0;i<len;i++) {
  55.     if (*m1++ != *m2++) {
  56.       /* output if the new cannot appended */
  57.       if ( dc > 0 && ds+dc != i) {
  58.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  59.         dc = 0;
  60.       }
  61.       /* append */
  62.       if (dc == 0) ds = i;
  63.       t1[dc]   = *(m1-1);
  64.       t2[dc++] = *(m2-1);
  65.       /* output if buff full */
  66.       if (dc == maxpl) {
  67.         cmpout(t1,t2,o1+ds,o2+ds,dc);
  68.         dc=0;
  69.       }
  70.       diffs++;
  71.     }
  72.   }
  73.   /* output if bytes left in buff */
  74.   if (dc > 0 )
  75.     cmpout(t1,t2,o1+ds,o2+ds,dc);
  76.   return diffs;
  77. }
  78.  
  79. /**********************/
  80.  
  81. long getfilesize(FILE *fp) {
  82.   fseek(fp,0,SEEK_END);
  83.   return ftell(fp);
  84. }
  85.  
  86. /**********************/
  87.  
  88. int main(int argc, char *argv[]) {
  89.   FILE *fp1,*fp2;
  90.   int len1, len2, strt1=0, strt2=0, pos1, pos2;
  91.   static char b1[buflen], b2[buflen]; /* Amiga !!! */
  92.   int l, diffs=0;
  93.   
  94.   if (argc < 3 || argc >4 || (argc == 4 && (opt_quick=strcasecmp(argv[3],"quick")))) {
  95.     fprintf(stderr,"Cmp 0.1 (%s)\n",__DATE__);
  96.     fprintf(stderr,"usage: %s file file [QUICK]\n",argv[0]);
  97.     exit(20);
  98.   }
  99.   
  100.   if (NULL == (fp1 = fopen(argv[1],"r"))) error1;
  101.   if (NULL == (fp2 = fopen(argv[2],"r"))) error2;
  102.   len1 = getfilesize(fp1);
  103.   len2 = getfilesize(fp2);
  104.   if (fseek(fp1,strt1,SEEK_SET)) error1;
  105.   if (fseek(fp2,strt2,SEEK_SET)) error2;
  106.   pos1 = strt1; pos2 = strt2;
  107.  
  108.   l = buflen;
  109.   while (pos1!=len1 && pos2!=len2) {
  110.     if (len1-pos1 < l) l = len1-pos1;
  111.     if (len2-pos2 < l) l = len2-pos2;
  112.     if (1 != fread(b1, l, 1, fp1)) error1;
  113.     if (1 != fread(b2, l, 1, fp2)) error2;
  114.     diffs += cmp (b1, b2, pos1, pos2, l);
  115.     pos1 += l; pos2 += l;
  116.   }
  117.   
  118.   printf(diffs == 0 ? "files are equal\n" : "files have %d differences\n",diffs);
  119.   if (len1 != len2) printf("file '%s' is %d bytes %s than file '%s'\n",
  120.     argv[1],abs(len1-len2),len1>len2?"larger":"shorter",argv[2]);
  121.   
  122.   if (diffs == 0) return 0; else return 5;
  123. }
  124.  
  125.